<?php
Doo::loadCore("controller/DooModule");
class frbMessage_MainController extends DooController implements DooModule {

	private $name = 'frb_Message';
	private $col = 1;
	private $max_row = 10;
	//$subMenu contient le nom des subMenu ainsi que les functions appelées pour y accéder
	private static $subMenu = array("inbox"=>"index", "relate"=>"relate");

	public function __construct(){
		$this->view()->setTagClass("TemplateTag", "globalbo");
	}

	/**
	 *
	 * Retourne les différents menu qui composent le module
	 * @return array nom des modules
	 */
	public static function getSubMenu() {
		return frbMessage_MainController::$subMenu;
	}


	/**
	 *
	 * Retourne le widget du module
	 * @return Array [name]=>[contenu] contenu du widget
	 */
	public function getWidget(){
		$count = $this->countNewMessage();
		return array
		(
					'title' 		=> 	'Gestion des messages',
					'content' 		=>	$count.' nouveaux messages',	
					'contentTitle'	=>	'Messages',
					'properties'	=> 	$this->col
		);

	}

	public function getMenu(){
		$count = $this->countNewMessage();
		return "Message<div class='new_message_menu'>$count</div>";
	}

	/**
	 *
	 * Retourne les fichiers à charger dans le header pour le module
	 * Actuellement sont géré css et js
	 * @return array 'css'=>NomFichier 'js'=>NomFichier 'custom'=>"Toute la balise"
	 */
	public function getModuleHeader(){
		return array("css"=>"css-message");
	}

	public function index(){
		Doo::loadModelAt('FrbMessage', 'frbMessage');
		$message = new FrbMessage();
		if($this->isAjax()){
			if($this->isPost()){
				if(!empty($_POST["action"]) && $_POST["action"] == "del" && !empty($_POST["id"]) && is_numeric($_POST["id"])){
					$message->mes_id = $_POST['id'];
					$this->db()->delete($message);
					echo "Message supprimer";
					return;
				}
			}
			echo "Erreur lors de la suppression du message";
			return;
		}
		$data['baseurl'] = Doo::conf()->APP_URL;
		$data['total'] = ceil($message->count()/$this->max_row);
		if($data['total'] == 0) $data['total'] = 1;
		$page = (!empty($_GET['page']) && is_numeric($_GET['page']) && $_GET['page'] >0 && $_GET['page']<=$data['total']) ? $_GET['page'] : 1;
		$rows = ($page-1) * $this->max_row;
		$data['mailInbox'] = $this->db()->find($message, array('desc'=>'mes_date', 'limit'=>"$rows, $this->max_row"));;
		$data['previous_page'] = $page-1;
		$data['next_page'] = ($page+1 >$data['total']) ? $data['total'] : $page+1;
		$data['page'] = $page;

		/*foreach ($data['mailInbox'] as $mail){
			$mail->mes_mes = html_entity_decode($mail->mes_mes);
			}*/

		$this->render('inbox', $data);
	}

	public function readMessage(){
		if($this->isAjax()){
			if($this->isPost()){
				Doo::loadModelAt('FrbMessage', 'frbMessage');
				$message = new FrbMessage();
				$message->mes_id = $_POST['id'];
				$message = $this->db()->find($message, array('limit'=>1));
				//echo $message->mes_id;
				$message->mes_flag = 0;
				$this->db()->update($message);
			}
		}
	}

	
	
	private function countNewMessage(){
		Doo::loadModelAt('FrbMessage', 'frbMessage');
		$message = new FrbMessage();
		$message->mes_flag = 1;
		return FrbMessage::_count($message);
		
	}

	private function isPost() {
		if ($_SERVER['REQUEST_METHOD'] == "POST") return true;
		else return false;
	}

}